define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var panel = $($(this).attr("href"));
                if (panel.length > 0) 
                {
                    Controller[panel.attr("id")].call(this);

                    $(this).on('click', function (e) 
                    {
                        $($(this).attr("href")).find(".btn-refresh").trigger("click");
                    });
                }

                //移除绑定的事件
                $(this).unbind('shown.bs.tab');
            });
            
            //必须默认触发shown.bs.tab事件
            $('ul.nav-tabs li.active a[data-toggle="tab"]').trigger("shown.bs.tab");
            Controller.api.bindevent();
        },
        info:function(){
            // 初始化表格参数配置
            var ids = Fast.api.query('ids');
            Table.api.init({
                extend: {
                    index_url: `subject/info/index?ids=${ids}`,
                    table: 'business',
                }
            });

            var table = $("#InfoTable");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search : false,
                toolbar: '#InfoToolbar',
                fixedColumns: true, 
                commonSearch: false,//普通搜索
                cardView: true,     //卡片
                showToggle: false,  //切换表格卡片
                showColumns: false, //可选列
                showExport: false,  //导出
                pagination: false,  //分页
                escape: false,
                columns: [
                    [
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: __('Title')},
                        {field: 'content', title: __('Content'), table: table, class: 'autocontent', formatter: Table.api.formatter.content,width:'100px'},
                        {field: 'thumbs', title: __('Thumbs'), events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'price', title: __('Price')},
                        {field: 'likes_num', title: __('Likes')},
                        {field: 'category.name', title: __('Category')},
                        {field: 'teacher.name', title: __('Teacher')},
                        {
                            field: 'createtime', 
                            title: __('CreateTime'), 
                            formatter: Table.api.formatter.datetime
                        },
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        order: function(){
            var ids = Fast.api.query('ids');
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: `subject/info/order?ids=${ids}`,
                    table: 'subject_order',
                }
            });

            var table = $("#OrderTable");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'createtime', //排序的字段名
                sortOrder: 'desc', //排序的方式
                toolbar: '#OrderToolbar',
                fixedColumns: true, 
                search: false,
                columns: [
                    [
                        { field: 'code', title: __('SubjectCode'), sortable: false, operate: 'LIKE' },
                        { field: 'subject.title', title: __('Subject'), sortable: true, operate: 'LIKE' },
                        { field: 'business.nickname', title: __('Business'), sortable: true, operate: 'LIKE' },
                        {field: 'total', title: __('Total(￥)'), operate: '='},
                        {
                            field: 'pay', 
                            title: __('Pay'), 
                            formatter: Table.api.formatter.status,
                            custom:{ 'money': 'red','zfb': 'blue', 'wx': 'green'},
                            searchList: {'money':__('Money'),'zfb':__('Zfb'),'wx':__('Wx')}, 
                        },
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                    ]
                ]
            });
            Table.api.bindevent(table);
        },
        comment: function(){
            var ids = Fast.api.query('ids');
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: `subject/info/comment?ids=${ids}`,
                    table: 'subject_comment',
                }
            });

            var table = $("#CommentTable");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'createtime', //排序的字段名
                sortOrder: 'desc', //排序的方式
                toolbar: '#CommentToolbar',
                fixedColumns: true, 
                search: false,
                columns: [
                    [
                        { field: 'business.nickname', title: __('Business'), sortable: true, operate: 'LIKE' },
                        { field: 'order.code', title: __('OrderCode'), sortable: true, operate: 'LIKE' },
                        {field: 'content', title: __('Content'),operate:'LIKE',formatter:Table.api.formatter.content, table: table, class: 'autocontent'},
                        {field: 'rate', title: __('Rate'), operate: '='},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                    ]
                ]
            });
            Table.api.bindevent(table);
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
        }
    };
    return Controller;
});
